home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / MPW Related / MPW Interfaces / CIncludes / iostream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-17  |  14.9 KB  |  627 lines  |  [TEXT/MPS ]

  1. /*ident    "@(#)ctrans:incl/iostream.h    1.1.7.9" */
  2. /**************************************************************************
  3.                         Copyright (c) 1984 AT&T
  4.                           All Rights Reserved   
  5.  
  6.         THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
  7.       
  8.         The copyright notice above does not evidence any        
  9.         actual or intended publication of such source code.
  10.  
  11. *****************************************************************************/
  12. #ifndef __IOSTREAM__
  13. #define __IOSTREAM__
  14.  
  15. // #include <memory.h>
  16. #include <String.h> // for Macintosh
  17.         /* Some inlines use memcpy */
  18.  
  19. /* If EOF is defined already verify that it is -1.  Otherwise
  20.  * define it.
  21.  */
  22.  
  23. #ifdef EOF
  24. #    if EOF!=-1
  25. #        define EOF (-1) 
  26. #    endif
  27. #else
  28. #    define EOF (-1)
  29. #endif
  30.  
  31. /* Don't worry about NULL not being 0 */
  32. #ifndef NULL
  33. #    define NULL 0
  34. #endif
  35.  
  36. #define    zapeof(c) ((c)&0377)
  37.         /* extracts char from c. The critical requirement is
  38.          *      zapeof(EOF)!=EOF
  39.          * ((c)&0377) and ((unsigned char)(c)) are alternative definitions
  40.          * whose efficiency depends on compiler environment.
  41.          */
  42.  
  43. typedef long streampos ;
  44. typedef long streamoff ;
  45.  
  46. class streambuf ;
  47. class ostream ;
  48.  
  49.  
  50. class ios {
  51. public: /* Some enums are declared in ios to avoid pollution of
  52.      * global namespace
  53.      */
  54.     enum io_state    { goodbit=0, eofbit=1, failbit=2, badbit=4,
  55.                 hardfail=0200};
  56.                 /* hard fail can be set and reset internally,
  57.                  * but not via public function */
  58.     enum open_mode    { in=1, out=2, ate=4, app=010, trunc=020,
  59.                 nocreate=040, noreplace=0100} ;
  60.     enum seek_dir    { beg=0, cur=1, end=2 } ;
  61.  
  62.     /* flags for controlling format */
  63.     enum        { skipws=01,    
  64.                     /* skip whitespace on input */
  65.               left=02,  right=04, internal=010,
  66.                     /* padding location */
  67.               dec=020, oct=040, hex=0100, 
  68.                     /* conversion base */
  69.               showbase=0200, showpoint=0400, uppercase=01000,
  70.               showpos=02000, 
  71.                     /* modifiers */
  72.               scientific=04000, fixed=010000,
  73.                     /* floating point notation */
  74.               unitbuf=020000, stdio=040000
  75.                     /* stuff to control flushing */
  76.               } ;
  77.     static const long 
  78.             basefield ; /* dec|oct|hex */
  79.     static const long
  80.             adjustfield ; /* left|right|internal */
  81.     static const long
  82.             floatfield ; /* scientific|fixed */
  83. public:
  84.             ios(streambuf*) ;
  85.     virtual        ~ios() ;
  86.  
  87.     long        flags()     { return x_flags ; }
  88.     long        flags(long f);
  89.  
  90.     long        setf(long setbits, long field);
  91.     long        setf(long) ;
  92.     long        unsetf(long) ;
  93.  
  94.     int        width()        { return x_width ; }
  95.     int        width(int w)
  96.     {
  97.             int i = x_width ; x_width = w ; return i ;
  98.     }
  99.         
  100.     ostream*    tie(ostream* s); 
  101.     ostream*    tie()        { return x_tie ; }
  102.     char        fill(char) ;
  103.     char        fill()         { return x_fill ; }
  104.     int        precision(int) ;
  105.     int        precision()    { return x_precision ; }
  106.  
  107.     int        rdstate()    { return state ; }
  108.             operator void*()
  109.                 {
  110.                 if (state&(failbit|badbit|hardfail)) return 0 ;
  111.                 else return this ;
  112.                 }
  113.  
  114.     int        operator!()
  115.                 { return state&(failbit|badbit|hardfail); } 
  116.     int        eof()    { return state&eofbit; }
  117.     int        fail()    { return state&(failbit|badbit|hardfail); }
  118.     int        bad()    { return state&badbit ; }
  119.     int        good()    { return state==0 ; }
  120.     void        clear(int i =0) 
  121.                 {    
  122.                 state =  (i&0377) | (state&hardfail) ;
  123.                 ispecial = (ispecial&~0377) | state ; 
  124.                 ospecial = (ospecial&~0377) | state ; 
  125.                 }
  126.     streambuf*    rdbuf() { return bp ;} 
  127.  
  128. public: /* Members related to user allocated bits and words */
  129.     long &        iword(int) ;
  130.     void* &        pword(int) ;
  131.     static long    bitalloc() ;
  132.     static int    xalloc() ;
  133.  
  134. private: /*** privates for implemting allocated bits and words */ 
  135.     static long    nextbit ;
  136.     static long    nextword ;
  137.     
  138.     int        nuser ;
  139.     union ios_user_union*
  140.             x_user ;
  141.     void    uresize(int) ;
  142. public: /* static member functions */
  143.     static void    sync_with_stdio() ;
  144. protected:
  145.     enum         { skipping=01000, tied=02000 } ;
  146.             /*** bits 0377 are reserved for userbits ***/
  147.     streambuf*    bp;
  148.     void        setstate(int b)
  149.             {    state |= (b&0377) ;
  150.                 ispecial |= b&~skipping ;
  151.                 ispecial |= b ;
  152.             }
  153.     int        state;    
  154.     int        ispecial;        
  155.     int        ospecial;
  156.     int        isfx_special;
  157.     int        osfx_special;        
  158.     int        delbuf;
  159.     ostream*    x_tie;
  160.     long         x_flags;
  161.     short        x_precision;
  162.     char        x_fill;
  163.     short         x_width;
  164.     
  165.     static void    (*stdioflush)() ;
  166.  
  167.     void        init(streambuf*) ;
  168.                 /* Does the real work of a constructor */
  169.             ios() ; /* No initialization at all. Needed by
  170.                  * multiple inheritance versions */
  171.     int        assign_private ;
  172.                 /* needed by with_assgn classes */
  173. private:        
  174.             ios(ios&) ; /* Declared but not defined */
  175.     void        operator=(ios&) ; /* Declared but not defined */
  176. public:   /* old stream package compatibility */
  177.     int        skip(int i) ; 
  178. };
  179.  
  180. class streambuf {
  181.     short        alloc;    
  182.     short        x_unbuf;
  183.     char*         x_base;    
  184.     char*        x_pbase;
  185.     char*        x_pptr;    
  186.     char*         x_epptr;
  187.     char*         x_gptr;
  188.     char*        x_egptr;
  189.     char*        x_eback;
  190.     int        x_blen;    
  191.     private:
  192.             streambuf(streambuf&); /* Declared but not defined */
  193.     void        operator=(streambuf&); /* Declared but not defined */
  194.     public:
  195.     void        dbp();
  196.     protected:
  197.     char*        base()         { return x_base ; }
  198.     char*        pbase()        { return x_pbase ; }
  199.     char*        pptr()         { return x_pptr ; }
  200.     char*        epptr()     { return x_epptr ; }
  201.     char*        gptr()         { return x_gptr ; }
  202.     char*        egptr()     { return x_egptr ; }
  203.     char*        eback()     { return x_eback ; }
  204.     char*         ebuf()        { return x_base+x_blen ; }
  205.     int        blen()        { return x_blen; }
  206.     void        setp(char*  p, char*  ep)
  207.     {
  208.         x_pbase=x_pptr=p ; x_epptr=ep ;
  209.     }
  210.     void        setg(char*  eb,char*  g, char*  eg)
  211.     {
  212.         x_eback=eb; x_gptr=g ; x_egptr=eg ;
  213.     }
  214.     void        pbump(int n) 
  215.     { 
  216.         x_pptr+=n ;
  217.     }
  218.  
  219.     void        gbump(int n) 
  220.     { 
  221.         x_gptr+=n ;
  222.         }
  223.  
  224.     void        setb(char* b, char* eb, int a = 0 )
  225.     {
  226.         if ( alloc && x_base ) delete x_base ;
  227.         x_base = b ;
  228.         x_blen= (eb>b) ? (eb-b) : 0 ;
  229.         alloc = a ;
  230.         }
  231.     int        unbuffered() { return x_unbuf; }
  232.     void        unbuffered(int unb) { x_unbuf = (unb!=0)  ; }
  233.     int        allocate()
  234.     {
  235.         if ( x_base== 0 && !unbuffered() ) return doallocate() ;
  236.         else                   return 0 ;
  237.     }
  238.     virtual int     doallocate();
  239.     public : 
  240.     virtual int    overflow(int c=EOF);
  241.     virtual int    underflow();
  242.     virtual int    pbackfail(int c);
  243.     virtual int    sync();
  244.     virtual streampos
  245. #ifdef __ATT2_1
  246.             seekoff(streamoff,ios::seek_dir,int =ios::in|ios::out);
  247. #else  /*__ATT2_1*/
  248.             seekoff(streamoff,seek_dir,int =ios::in|ios::out);
  249. #endif /*__ATT2_1*/
  250.     virtual streampos
  251.             seekpos(streampos, int =ios::in|ios::out) ;
  252.     virtual int    xsputn(const char*  s,int n);
  253.     virtual int    xsgetn(char*  s,int n);
  254.  
  255.     int        in_avail()
  256.     {
  257.         return x_gptr<x_egptr ? x_egptr-x_gptr : 0 ;
  258.     }
  259.  
  260.     int        out_waiting() 
  261.     {    
  262.         if ( x_pptr ) return x_pptr-x_pbase ;
  263.         else          return 0 ; 
  264.     }
  265.  
  266.     int        sgetc()
  267.     {
  268.         /***WARNING: sgetc does not bump the pointer ***/
  269.         return (x_gptr>=x_egptr) ? underflow() : zapeof(*x_gptr);
  270.     }
  271.     int        snextc()
  272.     {
  273.         return (++x_gptr>=x_egptr)
  274.                 ? x_snextc()
  275.                 : zapeof(*x_gptr);
  276.     }
  277.     int        sbumpc()
  278.     {
  279.         return  ( x_gptr>=x_egptr && underflow()==EOF ) 
  280.                 ? EOF 
  281.                 : zapeof(*x_gptr++) ;
  282.     }
  283.     int        optim_in_avail()
  284.     {
  285.         return x_gptr<x_egptr ;
  286.     }
  287.     int        optim_sbumpc()
  288.     {
  289.         return  ( underflow()==EOF ) 
  290.                 ? EOF 
  291.                 : zapeof(*x_gptr++) ;
  292.     }
  293.     void        stossc()
  294.     {
  295.         if ( x_gptr++ > x_egptr ) underflow() ;
  296.     }
  297.  
  298.     int        sputbackc(char c)
  299.     {
  300.         if (x_gptr > x_eback ) {
  301.             if ( *--x_gptr == c ) return zapeof(c) ;
  302.             else               return zapeof(*x_gptr=c) ;
  303.         } else {
  304.             return pbackfail(c) ;
  305.         }
  306.     }
  307.  
  308.     int        sputc(int c)
  309.     {
  310.         return (x_pptr>=x_epptr) ? overflow(zapeof(c))
  311.                       : zapeof(*x_pptr++=c);
  312.     }
  313.     int        sputn(const char*  s,int n)
  314.      {
  315.         if ( n <= (x_epptr-x_pptr) ) {
  316.             memcpy(x_pptr,s,n) ;
  317.             pbump(n);
  318.             return n ;
  319.         } else {
  320.             return xsputn(s,n) ;
  321.         }
  322.     }
  323.     int        sgetn(char*  s,int n)
  324.     {
  325.         if ( n <= (x_egptr-x_gptr) ) {
  326.             memcpy(s,x_gptr,n) ;
  327.             gbump(n);
  328.             return n ;
  329.         } else {
  330.             return xsgetn(s,n) ;
  331.         }
  332.     }
  333.     virtual streambuf*
  334.             setbuf(char*  p, int len) ;
  335.        streambuf*    setbuf(unsigned char*  p, int len) ;
  336.  
  337.     streambuf*    setbuf(char*  p, int len, int count) ;
  338.                 /* obsolete third argument */
  339.               /*** Constructors -- should be protected ***/
  340.             streambuf() ;
  341.             streambuf(char*  p, int l) ;
  342.  
  343.             streambuf(char*  p, int l,int c) ;
  344.             /* 3 argument form is obsolete.
  345.              * Use strstreambuf.
  346.              */
  347.     virtual        ~streambuf() ;
  348. private:
  349.     int        x_snextc() ;
  350. };
  351.  
  352. class istream : virtual public ios {
  353. public: /* Constructor */
  354.             istream(streambuf*) ;
  355.     virtual        ~istream() ;
  356. public:    
  357.     int        ipfx(int noskipws=0)
  358.             {    if ( noskipws?(ispecial&~skipping):ispecial) {
  359.                     return do_ipfx(noskipws) ;
  360.                 } else return 1 ;
  361.             }
  362.     void        isfx() { } 
  363.     istream&    seekg(streampos p) ;
  364. #ifdef __ATT2_1
  365.     istream&    seekg(streamoff o, ios::seek_dir d) ;
  366. #else  /*__ATT2_1*/
  367.     istream&    seekg(streamoff o, seek_dir d) ;
  368. #endif /*__ATT2_1*/
  369.        streampos    tellg() ; 
  370.     istream&    operator>> (istream& (*f)(istream&))
  371.             {    return (*f)(*this) ; }
  372.     istream&    operator>> (ios& (*f)(ios&) ) ;
  373.     istream&    operator>>(char*);
  374.     istream&    operator>>(unsigned char*);
  375.     istream&    operator>>(unsigned char& c)
  376.             {    if ( !ispecial && bp->optim_in_avail() ) {
  377.                     c = bp->optim_sbumpc() ;
  378.                     return *this;
  379.                 }
  380.                 else {
  381.                     return (rs_complicated(c));
  382.                 }
  383.             }
  384.     istream&    operator>>(char& c)
  385.             {    if ( !ispecial && bp->optim_in_avail() ) {
  386.                     c = bp->optim_sbumpc() ;
  387.                     return *this;
  388.                 }
  389.                 else {
  390.                     return (rs_complicated(c));
  391.                 }
  392.             }
  393.     istream&    rs_complicated(unsigned char& c);
  394.     istream&    rs_complicated(char& c);
  395.     istream&    operator>>(short&);
  396.     istream&    operator>>(int&);
  397.     istream&    operator>>(long&);
  398.     istream&    operator>>(unsigned short&);
  399.     istream&    operator>>(unsigned int&);
  400.     istream&    operator>>(unsigned long&);
  401.     istream&    operator>>(float&);
  402.     istream&    operator>>(double&);
  403.     istream&    operator>>(extended&); // for Macintosh, add extended support
  404.     istream&    operator>>(comp&);     // for Macintosh, add comp support
  405.     istream&    operator>>(streambuf*);
  406.     istream&    get(char* , int lim, char delim='\n');
  407.     istream&    get(unsigned char* b,int lim, char delim='\n');
  408.     istream&    getline(char* b, int lim, char delim='\n');
  409.     istream&    getline(unsigned char* b, int lim, char delim='\n');
  410.     istream&    get(streambuf& sb, char delim ='\n');
  411.     istream&    get_complicated(unsigned char& c);
  412.     istream&    get_complicated(char& c);
  413.     istream&    get(unsigned char& c)
  414.             {
  415.                 if ( !(ispecial & ~skipping) && bp->optim_in_avail() ) {
  416.                     x_gcount = 1 ;
  417.                     c = bp->sbumpc() ;
  418.                     return *this;
  419.                 } else {
  420.                     return (get_complicated(c));
  421.                 }
  422.             }
  423.     istream&    get(char& c)
  424.             {
  425.                 if ( !(ispecial & ~skipping) && bp->optim_in_avail() ) {
  426.                     x_gcount = 1 ;
  427.                     c = bp->sbumpc() ;
  428.                     return *this;
  429.                 } else {
  430.                     return (get_complicated(c));
  431.                 }
  432.             }
  433.     int         get()
  434.             {
  435.                 int c ;
  436.                 if ( !ipfx(1) ) return EOF ;
  437.                 else {
  438.                     c = bp->sbumpc() ;
  439.                     if ( c == EOF ) setstate(eofbit) ;
  440.                     return c ;
  441.                     }
  442.             }
  443.     int        peek() 
  444.             {
  445.                 if ( ipfx(-1) ) return bp->sgetc() ;
  446.                 else        return EOF ;
  447.  
  448.             }
  449.     istream&    ignore(int n=1,int delim=EOF) ;
  450.     istream&    read(char*  s,int n);
  451.     istream&    read(unsigned char* s,int n) 
  452.             {
  453.                 return read((char*)s,n) ;
  454.             }
  455.     int        gcount() ;
  456.     istream&    putback(char c);
  457.     int        sync()    { return bp->sync() ; }
  458. protected:  
  459.     int        do_ipfx(int noskipws) ;
  460.     void        eatwhite() ;
  461.             istream() ;
  462. private: 
  463.     int        x_gcount ;
  464.     void         xget(char*  c) ;
  465. public: /*** Obsolete constructors, carried over from stream package ***/
  466.             istream(streambuf*, int sk, ostream* t=0) ;
  467.                 /* obsolete, set sk and tie
  468.                  * via format state variables */
  469.             istream(int size ,char*,int sk=1) ;
  470.                 /* obsolete, use strstream */
  471.             istream(int fd,int sk=1, ostream* t=0) ;
  472.                 /* obsolete use fstream */
  473. };
  474.  
  475. class ostream : virtual public ios {
  476. public: /* Constructor */
  477.             ostream(streambuf*) ;
  478.     virtual        ~ostream();
  479. public:    
  480.     int        opfx()    /* Output prefix */
  481.             {    if ( ospecial )    return do_opfx() ;
  482.                 else        return 1 ;
  483.             }
  484.     void        osfx()
  485.             {    if ( osfx_special ) do_osfx() ; }
  486.             
  487.     ostream&    flush() ;
  488.     ostream&    seekp(streampos p) ;
  489. #ifdef __ATT2_1
  490.     ostream&    seekp(streamoff o, ios::seek_dir d) ;
  491. #else  /*__ATT2_1*/
  492.     ostream&    seekp(streamoff o, seek_dir d) ;
  493. #endif /*__ATT2_1*/
  494.      streampos    tellp() ; 
  495.     ostream&    put(char c) ;
  496.     ostream&    complicated_put(char c);
  497.     ostream&    operator<<(char c)
  498.     {
  499.         if (ospecial || osfx_special) {
  500.             return ls_complicated(c);
  501.         }
  502.         else {
  503.             if (  bp->sputc(c) == EOF )  {
  504.                 setstate(eofbit|failbit) ;
  505.             }
  506.             return *this ;
  507.         }
  508.     }
  509.  
  510.     ostream&    operator<<(unsigned char c) 
  511.     {
  512.         if (ospecial || osfx_special) {
  513.             return ls_complicated(c);
  514.         }
  515.         else {
  516.             if (  bp->sputc(c) == EOF )  {
  517.                 setstate(eofbit|failbit) ;
  518.             }
  519.             return *this ;
  520.         }
  521.     }
  522.     ostream&     ls_complicated(char);
  523.     ostream&     ls_complicated(unsigned char);
  524.  
  525.     ostream&    operator<<(const char*);
  526.     ostream&    operator<<(int a); 
  527.     ostream&    operator<<(long);    
  528.     ostream&    operator<<(float d) { return (*this)<<(extended) d; } // for Macintos    
  529.     ostream&    operator<<(double d) { return (*this)<<(extended) d; } // for Macintosh
  530.     ostream&    operator<<(extended); // for Macintosh
  531.     ostream&    operator<<(unsigned int a);
  532.     ostream&    operator<<(unsigned long);
  533.     ostream&    operator<<(void*);
  534.     ostream&    operator<<(streambuf*);
  535.     ostream&    operator<<(short i) { return *this << (int)i ; }
  536.     ostream&    operator<<(unsigned short i) 
  537.             { return *this << (int)i  ; }
  538.  
  539.     ostream&    operator<< (ostream& (*f)(ostream&))
  540.             { return (*f)(*this) ; }
  541.     ostream&    operator<< (ios& (*f)(ios&) ) ;
  542.  
  543.     ostream&    write(const char*  s,int n)    
  544.     {
  545.         if ( !state ) {
  546.             if ( bp->sputn(s,n) != n ) setstate(eofbit|failbit);
  547.             }
  548.         return *this ;
  549.     }
  550.     ostream&    write(const unsigned char* s, int n)
  551.     {
  552.         return write((const char*)s,n);
  553.     }
  554. protected: /* More ostream members */
  555.     int        do_opfx() ;
  556.     void    do_osfx() ;
  557.             ostream() ;
  558.  
  559. public: /*** Obsolete constructors, carried over from stream package ***/
  560.             ostream(int fd) ;
  561.                 /* obsolete use fstream */
  562.             ostream(int size ,char*) ;
  563.                 /* obsolete, use strstream */
  564. } ;
  565.  
  566. class iostream : public istream, public ostream {
  567. public:
  568.             iostream(streambuf*) ;
  569.     virtual        ~iostream() ;
  570. protected:
  571.             iostream() ;
  572.     } ;
  573.  
  574. class Iostream_init;  // for Macintosh
  575.  
  576. class istream_withassign : public istream {
  577. public:
  578.             istream_withassign() ;
  579.             istream_withassign(Iostream_init*); // for Macintosh
  580.     virtual        ~istream_withassign() ;
  581.     istream_withassign&    operator=(istream&) ;
  582.     istream_withassign&    operator=(streambuf*) ;
  583. } ;
  584.  
  585. class ostream_withassign : public ostream {
  586. public:
  587.             ostream_withassign() ;
  588.             ostream_withassign(Iostream_init*); // for Macintosh                        
  589.     virtual        ~ostream_withassign() ;
  590.     ostream_withassign&    operator=(ostream&) ;
  591.     ostream_withassign&    operator=(streambuf*) ;
  592. } ;
  593.  
  594. class iostream_withassign : public iostream {
  595. public:
  596.             iostream_withassign() ;
  597.     virtual        ~iostream_withassign() ;
  598.     iostream_withassign&    operator=(ios&) ;
  599.     iostream_withassign&    operator=(streambuf*) ;
  600. } ;
  601.  
  602. extern istream_withassign cin ;
  603. extern ostream_withassign cout ;
  604. extern ostream_withassign cerr ;
  605. extern ostream_withassign cdebug ;
  606.  
  607. ios&        dec(ios&) ; 
  608. ostream&    endl(ostream& i) ;
  609. ostream&    ends(ostream& i) ;
  610. ostream&    flush(ostream&) ;
  611. ios&        hex(ios&) ;
  612. ios&        oct(ios&) ; 
  613. istream&    ws(istream&) ;
  614.  
  615. class Iostream_init { // for Macintosh: This used to define
  616.                       // a static instance; it is now defined
  617.                       // in cstreams.c.
  618.     static int    stdstatus ; /* see cstreams.c */
  619.     // static int    initcount ; deleted for Macintosh
  620.     friend        ios ;
  621. public:
  622.     Iostream_init() ; 
  623.     ~Iostream_init() ; 
  624. }; // iostream_init ; deleted for Macintosh    
  625.  
  626. #endif
  627.